package com.huiyeji.hui.controller;

import com.huiyeji.hui.auth.Authenticator;
import com.huiyeji.hui.auth.License;
import com.huiyeji.hui.entity.User;
import com.huiyeji.hui.service.ApplicationService;
import com.huiyeji.hui.utils.HuiResponse;
import com.huiyeji.hui.utils.PatternUtil;
import com.huiyeji.hui.utils.SuccessEnum;
import com.huiyeji.hui.vo.PageResult;
import com.huiyeji.hui.vo.SelfAndOthersAndGroupsVo;
import com.huiyeji.hui.vo.SimpleGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author zhaoduo
 */
@RestController
@RequestMapping("/group")
public class GroupController {

    private final ApplicationService service;

    @Autowired
    public GroupController(ApplicationService service){
        this.service =  service;
    }

    private final Authenticator authenticator = Authenticator.getInstance();

    @HuiAnno("大王")
    @PostMapping("/changeGroupName")
    public HuiResponse<?> changeGroupName(@RequestParam("gid")Long gid,
                                       @RequestParam("name")String groupName,
                                       HttpSession session){
        User user = authenticator.check(session, License.GENERAL);
        PatternUtil.test(PatternUtil.PATTERN_NAME,groupName);
        service.changeGroupName(user.getUid(),gid,groupName);
        return HuiResponse.ok(SuccessEnum.MODIFY_SUCCESS);
    }

    @PostMapping("/changeGroupNotice")
    public HuiResponse<?> changeNotice(@RequestParam("gid")Long gid,
                                       @RequestParam("notice")String notice,
                                       HttpSession session){
        User user = authenticator.check(session, License.GENERAL);
        service.changeGroupNotice(user.getUid(),gid,notice);
        return HuiResponse.ok(SuccessEnum.MODIFY_SUCCESS);
    }

    @GetMapping("/keySearch")
    public HuiResponse<?> keySearch(@RequestParam Map<String,String> params,
                                    HttpSession session){
        User user = authenticator.check(session, License.GENERAL);
        PageResult<SimpleGroupVo> result = service.groupKeySearch(user.getUid(),params);
        return HuiResponse.ok(result);
    }

    @GetMapping("/conditionSearch")
    public HuiResponse<?> conditionSearch(@RequestParam Map<String,String> params,
                                          HttpSession session){
        User user = authenticator.check(session, License.GENERAL);
        PageResult<SimpleGroupVo> result = service.groupConditionSearch(user.getUid(),params);
        return HuiResponse.ok(result);
    }

    @DeleteMapping("/remove/{gid}")
    public HuiResponse<?> remove(@PathVariable("gid") Long gid,
                                 HttpSession session){
        User user = authenticator.check(session, License.GENERAL);
        service.removeGroup(user.getUid(),gid);
        return HuiResponse.ok(SuccessEnum.REMOVE_GROUP_SUCCESS);
    }

    @PutMapping("/createGroup")
    public HuiResponse<?> createGroup(HttpSession session,
                                      @RequestParam("name")String groupName){
        User user = authenticator.check(session, License.GENERAL);
        service.createGroup(user.getUid(),groupName);
        return HuiResponse.ok(SuccessEnum.CREATE_SUCCESS);
    }

    @GetMapping("/getGroup/{gid}")
    public HuiResponse<?> getGroup(@PathVariable("gid")Long gid,
                                   HttpSession session){
        User user = authenticator.check(session, License.GENERAL);
        SelfAndOthersAndGroupsVo.Group group = service.getGroup(user.getUid(),gid);
        return HuiResponse.ok(group);
    }
}
